package pkg

import (
	"encoding/json"
	"fmt"
	"github.com/Baidu-AIP/golang-sdk/aip/censor"
)

const (
	ak = "ALTAKZjA6q8OS0WUwXQnJV3o6Z"
	sk = "6406a9d3e9de4bc89b3438dbd96fffe2"
)

type BaiDuSuccess struct {
	LogId      int    `json:"log_id"`
	Conclusion string `json:"conclusion"`
	Data       struct {
		Msg         string  `json:"msg"`
		Probability float64 `json:"probability"`
		Type        int     `json:"type"`
		Words       string  `json:"words,omitempty"`
	} `json:"data"`
}

type BaiDuError struct {
	LogId          int    `json:"log_id"`
	Conclusion     string `json:"conclusion"`
	ConclusionType int    `json:"conclusionType"`
	Data           []struct {
		Type           int    `json:"type"`
		SubType        int    `json:"subType"`
		Conclusion     string `json:"conclusion"`
		ConclusionType int    `json:"conclusionType"`
		Msg            string `json:"msg"`
		Hits           []struct {
			DatasetName string   `json:"datasetName"`
			Words       []string `json:"words"`
			Probability float64  `json:"probability,omitempty"`
		} `json:"hits"`
	} `json:"data"`
}

func SensitiveWords(text string) string {
	client := censor.NewCloudClient(ak, sk)
	res := client.TextCensor(text)
	var baiDu BaiDuSuccess
	var baiDuError BaiDuError
	err := json.Unmarshal([]byte(res), &baiDu)
	if err != nil {
		err = json.Unmarshal([]byte(res), &baiDuError)
		if err != nil {
			fmt.Println(err)
			return "敏感词检测失败"
		}
		for _, datum := range baiDuError.Data {
			return "备注" + baiDuError.Conclusion + datum.Msg
		}
	}
	if baiDu.Conclusion == "合规" {
		return baiDu.Conclusion
	}
	return baiDu.Data.Msg
}

/*package pkg

// TrieNode 字典树节点
type TrieNode struct {
	children map[rune]*TrieNode
	isEnd    bool // 是否敏感词结束节点
}

// NewTrieNode 创建新节点
func NewTrieNode() *TrieNode {
	return &TrieNode{
		children: make(map[rune]*TrieNode),
		isEnd:    false,
	}
}

// SensitiveFilter 敏感词过滤器
type SensitiveFilter struct {
	root *TrieNode
}

// NewSensitiveFilter 创建过滤器
func NewSensitiveFilter() *SensitiveFilter {
	return &SensitiveFilter{root: NewTrieNode()}
}

// AddWord 添加敏感词
func (sf *SensitiveFilter) AddWord(word string) {
	node := sf.root
	for _, char := range word {
		if _, exists := node.children[char]; !exists {
			node.children[char] = NewTrieNode()
		}
		node = node.children[char]
	}
	node.isEnd = true
}

// Filter 过滤文本并替换敏感词
func (sf *SensitiveFilter) Filter(text string) string {
	runes := []rune(text)
	result := make([]rune, len(runes))
	copy(result, runes)

	for i := 0; i < len(runes); i++ {
		node := sf.root
		for j := i; j < len(runes); j++ {
			char := runes[j]
			// 检查当前字符是否在树中
			if child, exists := node.children[char]; exists {
				node = child
				// 发现完整敏感词
				if node.isEnd {
					for k := i; k <= j; k++ {
						result[k] = '*'
					}
					i = j // 跳过已处理部分
					break
				}
			} else {
				break
			}
		}
	}
	return string(result)
}

func main() {
	filter := NewSensitiveFilter()

	// 添加敏感词
	filter.AddWord("暴力")
	filter.AddWord("色情")
	filter.AddWord("毒品")

	//text := "严禁传播暴力、色情和毒品相关内容"
}
*/
